package com.example.controller;

import com.example.service.BorderSize;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class BorderListener implements BorderSize, MouseListener, ActionListener {
    int x,y;
    JPanel panel;
    JComboBox JCB;
    ChessBorder CHB;
    int count = 0;
    String fightType = "人人对战";
    int huiX,huiY;
    int max,row,colum;	//人机大战需要用的数据

    //获取画布
    public void setPanel(JPanel panel){
        this.panel = panel;
    }
    //传递JComboBox
    public void setJCB(JComboBox JCB){
        this.JCB = JCB;
    }
    //传递ChessBorder
    public void setCHB(ChessBorder CHB){
        this.CHB = CHB;
    }
    public void actionPerformed(ActionEvent e){
        String action = e.getActionCommand();
        if("开始新游戏".equals(action)){
            //清空棋盘
            for(int i=0;i<ROW;i++){
                for(int j=0;j<COLUM;j++){
                    chessArray[i][j] = 0;
                }
            }
            //设置JComboBox不可用
            JCB.setEnabled(false);
            panel.addMouseListener(this);
            //画的时候获取画布（游戏重新开始后清空画板）
            Graphics g = panel.getGraphics();
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            CHB.paint(g);
        }else if("认输".equals(action)){
            if(count==0){
                JOptionPane.showMessageDialog(null, "白棋获胜！！！");
            }else {
                JOptionPane.showMessageDialog(null, "黑棋获胜！！！");
            }
            //黑棋先下
            count = 0;
            JCB.setEnabled(true);
            panel.removeMouseListener(this);
        }else if("悔棋".equals(action)){
            if(chessArray[huiX][huiY]==0){
                return;
            }
            chessArray[huiX][huiY] = 0;
            if("人机对战".equals((String)JCB.getSelectedItem())){
                chessArray[row][colum] = 0;
                if(count==0){
                    count = 1;
                }else {
                    count = 0;
                }
            }
            Graphics g = panel.getGraphics();
            //抗锯齿效果
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            if(count==0){
                count = 1;
            }else {
                count = 0;
            }
            CHB.paint(g);
        }else {
            String s =(String)JCB.getSelectedItem();
            fightType = s;
        }
    }

    public void mouseReleased(MouseEvent e){
        x = e.getX();
        y = e.getY();
        Result result = new Result();
        if(panel!=null){
            Graphics g = panel.getGraphics();
            //抗锯齿处理	添加此行后，棋子边缘光滑
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            int index = 0;
            int r,c;
            for(int i=X;i<=(ROW-1)*SIZE;i=i+SIZE){
                for(int j=Y;j<=(COLUM-1)*SIZE;j=j+SIZE){
                    if((x-i>-SIZE/2&&x-i<SIZE/2)&&(y-j>-SIZE/2&&y-j<SIZE/2)){
                        r = (i-X)/SIZE;
                        c = (j-Y)/SIZE;
                        //画黑棋
                        if(count==0 && chessArray[r][c]!=1 && chessArray[r][c]!=2){
                            chessArray[r][c] = 1;
                            g.setColor(Color.BLACK);
                            g.fillOval(i-SIZE/2, j-SIZE/2, SIZE, SIZE);
                            count++;	//转换到白棋
                            result.r = r;
                            result.c = c;
                            //悔棋的棋子
                            huiX = r;
                            huiY = c;
                        }
                        //画白棋
                        if(count==1 && chessArray[r][c]!=1 && chessArray[r][c]!=2){
                            chessArray[r][c] = 2;
                            g.setColor(Color.WHITE);
                            g.fillOval(i-SIZE/2, j-SIZE/2, SIZE, SIZE);
                            count--;	//转换到黑棋
                            result.r = r;
                            result.c = c;
                            //悔棋的棋子
                            huiX = r;
                            huiY = c;
                        }

                    }
                }
            }
            //（人人对战判断黑白棋胜利）和（人机对战判断黑棋部分）
            if(result.WinOrLoss()){
                if(chessArray[result.r][result.c]==1){
                    JOptionPane.showMessageDialog(null, "黑棋获胜！");
                }
                if(chessArray[result.r][result.c]==2){
                    JOptionPane.showMessageDialog(null, "白棋胜利！");
                }
                //重新开始后黑棋先下
                count = 0;
                //游戏结束后设置下拉列表框为可用
                JCB.setEnabled(true);
                //游戏结束后不可继续下棋  （移除鼠标监听器）
                panel.removeMouseListener(this);
            }
            //人机对战部分
            if("人机对战".equals(fightType)){
                AI ai = new AI();
                ai.AIplay();
                int max=0;
                //寻找最大权重
                for(int i=0;i<ROW;i++){
                    for(int j=0;j<COLUM;j++){
                        if(weightArray[i][j]>max){
                            max = weightArray[i][j];
                            row = i;
                            colum = j;
                        }
                        System.out.print(weightArray[i][j]+" ");
                    }
                    System.out.println();
                }
                System.out.println(max);
                //人机对战中画电脑的棋子
                if(count==1){
                    chessArray[row][colum] = 2;
                    g.setColor(Color.WHITE);
                    System.out.println(row+" "+colum);
                    g.fillOval(row*SIZE+X-SIZE/2,colum*SIZE+Y-SIZE/2 ,SIZE, SIZE);
                    count--;
                }
                result.r = row;
                result.c = colum;
            }
            if(result.WinOrLoss()&&"人机对战".equals(fightType)){
                if(chessArray[row][colum]==2){
                    JOptionPane.showMessageDialog(null, "白棋胜利！");
                    row = 0;
                    colum = 0;
                }
                //重新开始后黑棋先下
                count = 0;
                //游戏结束后设置下拉列表框为可用
                JCB.setEnabled(true);
                //游戏结束后不可继续下棋  （移除鼠标监听器）
                panel.removeMouseListener(this);

            }
        }
    }


    public void mouseClicked(MouseEvent e){}
    public void mousePressed(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
}
